/*
 * Copyright (c) 2021, Texas Instruments Incorporated
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * *  Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "ti_msp_dl_config.h"

#include "board.h"
#include <stdio.h>
#include "oled.h"
#include "hw_encoder.h"
#include "hw_timer.h"
#include "bsp_tb6612.h"
#include "mpu6050.h"
// #include "jy61p.h"
#include "control.h"
#include "xunji.h"

int L_speed = 0; // 左轮速度
int R_speed = 0; // 右轮速度

uint8_t rounds = 1;
char oled_buffer[16] = {0};
int key_pressed = 0; // 按键是否按下
uint8_t run_key_flag = 0; // 运行按键状态 0 IDLE 1 RUNNING


int main(void)
{
    SYSCFG_DL_init();
    // 开发板初始化
    // board_init();
    // 编码器初始化
    //  encoder_init();
    // oled初始化
    Set_Motor_PWM(0, 0);
    OLED_Init();
    delay_ms(5);
    OLED_Clear();
    // OLED_ShowString(0, 0, (uint8_t *)"Hello World!", 16, 1);
    OLED_Refresh();
    

    NVIC_EnableIRQ(GPIO_MULTIPLE_GPIOA_INT_IRQN);
    NVIC_EnableIRQ(TIMER_BEEP_INST_INT_IRQN);
    // sprintf(oled_buffer, "Rounds: %d", rounds);
    OLED_ShowString(0, 0, (uint8_t *)"EDC-CAR", 16, 1);
    sprintf(oled_buffer, "Rounds: %d", rounds);
    OLED_ShowString(0, 20, (uint8_t *)oled_buffer, 16, 1);
    OLED_Refresh();

    int a = 0;
    // PID_xunji_Init();
    // float delta_speed = 0;
    while (1)
    {
        if(key_pressed){
            key_pressed = 0; // 清除按键状态
            a++;
            DL_GPIO_setPins(GPIO_BEEP_PORT, GPIO_BEEP_A_PIN);
            DL_TimerG_startCounter(TIMER_BEEP_INST);
        }
        // encoder_update();

        // L_speed = get_encoder_count();
        // R_speed = get_encoder_count2();

        // delta_speed = 2 * xunji(); // 计算偏差
        // Set_Motor_PWM(100 - delta_speed, 100 + delta_speed);

        // sprintf(oled_buffer, "Rounds: %d", rounds);
        // OLED_ShowString(0, 0, oled_buffer, 16, 1);
        // OLED_Refresh();
        // delay_1ms(100);
    }
}

void TIMER_BEEP_INST_IRQHandler(void)
{
    switch (DL_TimerG_getPendingInterrupt(TIMER_BEEP_INST)) {
        case DL_TIMERG_IIDX_ZERO:
            DL_GPIO_clearPins(GPIO_BEEP_PORT, GPIO_BEEP_A_PIN);
            break;
        default:
            break;
    }
}